/**
 * 
 */
package co.fitstart.admin.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.annotation.Dynamic;

import co.fitstart.admin.dao.ContentDao;
import co.fitstart.admin.service.ActivityService;
import co.fitstart.entity.content.Activity;

/**
 * @author downpour
 *
 */
@Service("activityService")
public class ActivityServiceImpl extends DataServiceImpl<Activity> implements ActivityService {
    
    @Dynamic
    private ContentDao contentDao;

    /* (non-Javadoc)
     * @see co.fitstart.admin.service.content.ActivityService#online(co.fitstart.common.entity.content.Activity)
     */
    public void online(Activity activity) {
        genericDaoSupport.update(activity.online());
    }

    /* (non-Javadoc)
     * @see co.fitstart.admin.service.content.ActivityService#stick(co.fitstart.common.entity.content.Activity, int)
     */
    public void stick(Activity activity, int maxStickyLimit) {
        
        List<Activity> sticks = contentDao.listStickActivity();

        if(CollectionUtils.isEmpty(sticks)) { // no sticky ones
            
            genericDaoSupport.update(activity.stick(100));
            
        } else {
            
            // stick the activity using the 
            Activity last = sticks.get(sticks.size() - 1);
            genericDaoSupport.update(activity.stick(last.getSerial()));
            
        }
        
        if(sticks.size() == maxStickyLimit) { // unstick the first one when the sticky queue is full
            
            Activity first = sticks.get(0);
            genericDaoSupport.update(first.unstick());
            
        } 
        
    }

    /* (non-Javadoc)
     * @see co.fitstart.admin.service.content.ActivityService#unstick(co.fitstart.common.entity.content.Activity, int)
     */
    public void unstick(Activity activity) {
        genericDaoSupport.update(activity.unstick());
    }

}
